﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Diagnostics;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
 


/// <summary>
/// Summary description for DBObject
/// </summary>
public static class DBObject
{
	/*public DBObject()
	{
		//
		// TODO: Add constructor logic here
		//
	}*/

    public static string KickOffParsingOperation(string filename, string client)
    {//using the client and filename we will kick of a parsing operation on the backend of Oracle
     //the results of which will be fed back to the client as a series of potential Datastructure objects
     //the datastructure objects are used to identify the structure of the source file and primary keys.
        Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("RecRunner");
        OracleCommand cmd = new OracleCommand("pkg_utility.determinesourcestructure");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new OracleParameter("p_outStructure",OracleDbType.Varchar2,100));
        cmd.Parameters[0].Direction = ParameterDirection.Output;
        db.ExecuteNonQuery(cmd);


        string results = string.Format("{0}",
                               db.GetParameterValue(cmd, "p_outStructure"));
        return results;

    }
    public static DataTable RetrieveAllRecs(string s_sql)
    {        
        Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("RecRunner");
        DataTable dataTable = new DataTable();
        dataTable.Load(db.ExecuteReader(CommandType.Text, s_sql));
        return dataTable;
    }     

   public static ArrayList foobar()
   {
        ArrayList arrList = new ArrayList();
        OracleConnection conn = new OracleConnection();
        OracleDataReader reader = null;
        OracleCommand cmd = null;
        conn.ConnectionString = "User Id=recrunner;Password=recrunner;Data Source=STARTERDB;";
        try
        {
            conn.Open();
            string cmdQuery = "select * from all_recs";

            // Create the OracleCommand
            cmd = new OracleCommand(cmdQuery);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;

            // Execute command, create OracleDataReader object
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                // output Employee Name and Number
                //strOut += reader.GetString(0);
                string str = reader.GetString(0) + reader.GetString(1) +
                             reader.GetString(2) + reader.GetString(3) +
                             reader.GetString(4) + reader.GetString(5) +
                             reader.GetString(6);

                /*arrList.Add(new SavedReconciliation(reader.GetInt32(0),   //int id,
                                                    reader.GetString(1),  //string strname,
                                                    reader.GetString(2),  //string LTable
                                                    reader.GetString(3),  // string RTable
                                                    reader.GetString(4),  //string OTable
                                                    reader.GetDateTime(5),//LastRun
                                                    reader.GetInt32(6)    //int reclink
                                                    ));*/
            }

        }

        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message.ToString());
        }
        finally
        {

            reader.Dispose();
            cmd.Dispose();
            conn.Dispose();
        }
        return arrList;
    }
    public static string MakeConnection()
    {
        string strOut = "";
        OracleConnection conn = new OracleConnection();
        OracleDataReader reader = null;
        OracleCommand cmd = null;
        conn.ConnectionString = "User Id=recrunner;Password=recrunner;Data Source=STARTERDB;";
        try
        {
            conn.Open();
            string cmdQuery = "select * from dual";

            // Create the OracleCommand
            cmd = new OracleCommand(cmdQuery);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;

            // Execute command, create OracleDataReader object
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                // output Employee Name and Number
                strOut += reader.GetString(0);
            }

        }

        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message.ToString());
        }
        finally
        {

            reader.Dispose();
            cmd.Dispose();
            conn.Dispose();
        }
        return strOut;

    }
}